package com.kingdee.eas.hr.quali.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.emp.PersonPositionCollection;
import com.kingdee.eas.hr.emp.PersonPositionFactory;
import com.kingdee.eas.hr.quali.QualificationManagerCollection;
import com.kingdee.eas.hr.quali.QualificationManagerFactory;
import com.kingdee.eas.hr.quali.QualificationManagerInfo;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

public class QualificationManagerEditHandler extends EditHandler {
	
	
	
	@Override
	protected void afterSave(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)throws SHRWebException {
		super.afterSave(request, response, model);
		QualificationManagerInfo info = (QualificationManagerInfo) model;
		if(info.getId() == null){
			info.setName(""+System.currentTimeMillis());
			info.setNumber(""+System.currentTimeMillis());
		}
	}

	public String getFilterDataAction(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			QualificationManagerCollection colls = QualificationManagerFactory.getLocalInstance(ctx).getQualificationManagerCollection();
			if(colls.size() > 0){
				String ids = "('";
				for(int i = 0,size = colls.size(); i < size; i++){
					ids = ids + colls.get(i).getPerson().getId() + "','";
				}
				ids = ids.substring(0, ids.length()-2) + ")";
				data.put("status", 200);
				data.put("ids", ids);
			}
		} catch (BOSException e) {
			e.printStackTrace();
		}
		JSONUtils.writeJson(response, data);
		return null;
	}
	
	public String getDataInfoAction(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) throws SHRWebException {
		Context ctx = SHRContext.getInstance().getContext();
		Map<String, Object> data = new HashMap<String, Object>();
		String personId = request.getParameter("personId");
		try {
			PersonPositionCollection colls = PersonPositionFactory.getLocalInstance(ctx)
					.getPersonPositionCollection("select personDep.id,personDep.name,primaryPosition.id,primaryPosition.name where person.id = '"+personId+"'");
			if(colls.size() > 0){
				data.put("status", 200);
				data.put("orgId", colls.get(0).getPersonDep().getId());
				data.put("orgName", colls.get(0).getPersonDep().getName());
				data.put("positionId", colls.get(0).getPrimaryPosition().getId());
				data.put("positionName", colls.get(0).getPrimaryPosition().getName());
			}
		} catch (BOSException e) {
			e.printStackTrace();
		}
		JSONUtils.writeJson(response, data);
		return null;
	}
}
